package com.uyaer.magic.utils
{
	import com.uyaer.magic.res.URLType;

	public class Utils
	{
		/**
		 *随机产生从min - max的整数 
		 * @param min
		 * @param max
		 * @return 
		 * 
		 */             
		public static function rangInt(min:int , max:int):int{
			return Math.round(Math.random() * (max - min)) + min;
		}
		
		/**
		 *返回概率 
		 * @param p 0-1;
		 * @return 
		 * 
		 */		
		public static function probability(p:Number):Boolean{
			if(p>=1){
				return true;
			}else if(p<=0){
				return false;
			}
			return Math.random() < p;
		}
		
		/**
		 *随机产生1-dig的随机数，位数不够补0 
		 * @param dig
		 * @return 
		 * 
		 */             
		public static function rangString(dig:uint):String{
			dig = dig || 1;
			var max:uint = (dig + 1) * 10 - 1;
			var min:uint = 0;
			var r:String = rangInt(min , max).toString();
			if(r.length < dig){
				var temp:int = dig - r.length;
				var tempStr:String = "";
				for(var i:int = 0 ; i < temp ; i ++){
					tempStr += "0";
				}
				tempStr += r;
				return tempStr;
			}
			return r;
		}
		
		/**
		 *用 “0” 补全数值字符串 
		 * @param num
		 * @param dig
		 * @return 
		 * 
		 */             
		public static function complement(num:uint , dig:uint):String{
			var str:String = num.toString();
			if(str.length < dig){
				var temp:String = "";
				for(var i:int = 0 ; i < (dig - str.length); i++){
					temp += "0";
				}
				temp += str;
				return temp;
			}
			return str;
		}
		
		public static function now():Number{
			return new Date().time;
		}
		
		/**
		 *获得一个UUID 
		 * @return 
		 * 
		 */		
		public static function UUID():String{
			return now().toString(16)+rangString(4);
		}
		
		/**
		 *获得全名  名称+后缀 
		 * @param url
		 * @return 
		 * 
		 */		
		public static function getNameExt(url:String):String{
			return url.substring(url.lastIndexOf("/")+1);
		}
		
		/**
		 *获得名称 
		 * @param name
		 * @return 
		 * 
		 */		
		public static function getName(name:String):String{
			return name.substring(0,name.lastIndexOf(".")).toLocaleLowerCase();
		}
		
		/**
		 *获得后缀名 
		 * @param url
		 * @return 
		 * 
		 */			
		public static function getExt(url:String):String{
			return url.substring(url.lastIndexOf(".")+1).toLocaleLowerCase();
		}
		
		/**
		 *缓动  easeIn
		 * @param p
		 * @return 
		 * 
		 */		
		public static function easeIn(p):Number{
			return Math.pow(p, 2);
		}
		/**
		 *缓动  easeOut
		 * @param p
		 * @return 
		 * 
		 */	
		public static function easeOut(p):Number{
			return -(Math.pow((p-1), 2) -1);
		}
		
	}
}